#ifndef VTEXTEDIT_H
#define VTEXTEDIT_H

#include "../VCommon/VFlowLayout.h"

//////////////////////////////////////////////////////////////////////////
// Multi-line editor
class VMultilineEdit: public VFlowLayoutTextEdit
{
	Q_OBJECT	
public:
	VMultilineEdit(QWidget* parent=0);

protected:
	virtual void focusInEvent(QFocusEvent *e);
	virtual void focusOutEvent(QFocusEvent *e);

protected:
	/*virtual QSize sizeHint() const
	{
		QSize sizehint = document()->documentLayout()->documentSize().toSize();
		sizehint.setHeight(this->fitted_height);
		return sizehint;
	}

	virtual QSize minimumSizeHint() const
	{
		QSize sizehint = document()->documentLayout()->documentSize().toSize();
		sizehint.setHeight(this->fitted_height);
		return sizehint;
	}*/

	//dirty haaaaaaaackkkkkkkk
	virtual void showEvent(QShowEvent* event)
	{
		slotContentsChanged();
		VFlowLayoutTextEdit::showEvent(event);
	}

	virtual void keyPressEvent(QKeyEvent *e)
	{
		if (m_ignoreInput)
		{
			if (e->key() == Qt::Key_Space)
				emit keySpacePressed();

			e->ignore();			
		}
		else
			VFlowLayoutTextEdit::keyPressEvent(e);
	}

public:
	void setIgnoreInput(bool ignore) { m_ignoreInput = ignore; }
	bool ignoreInput() const { return m_ignoreInput; }

//public:
//	void toHtml(const QString& text)

signals:
	void editingStarted();
	void editingFinished();
	void keySpacePressed();
	//void signal

//public slots:		
//	void textChange();

private:
	int fitted_height;
	bool m_ignoreInput;
};

//////////////////////////////////////////////////////////////////////////
// VMultilineEditHelper
class VMultilineEditHelper: public QObject
{
	Q_OBJECT

public:
	VMultilineEditHelper(VMultilineEdit* editor, QObject* parent=0);

private:
	void setupTextActions();

private slots:
	void focusChange(QWidget* old, QWidget* now);
	void editorGotFocus();
	void editorLostFocus();

private slots:
	void textBold();
	void textUnderline();
	void textItalic();
	void textFamily(const QString &f);
	void textSize(const QString &p);
	void textStyle(int styleIndex);
	void textColor();
	void textAlign(QAction *a);
	void currentCharFormatChanged(const QTextCharFormat &format);
	void cursorPositionChanged();

private:
	void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
	void fontChanged(const QFont &f);
	void colorChanged(const QColor &c);
	void alignmentChanged(Qt::Alignment a);

private:
	QAction 
		*actionTextBold,
		*actionTextUnderline,
		*actionTextItalic,
		*actionTextColor,
		*actionAlignLeft,
		*actionAlignCenter,
		*actionAlignRight,
		*actionAlignJustify;

	QComboBox *comboStyle;
	QFontComboBox *comboFont;
	QComboBox *comboSize;

	QToolBar *tb;
	QWidget* tbOwner;
	QString fileName;
	VMultilineEdit *textEdit;
};

#endif // VTEXTEDIT_H
